package com.icode.gulimall.coupon.service.impl;

import com.icode.common.to.MemberPrice;
import com.icode.common.to.SkuReductionTo;
import com.icode.gulimall.coupon.entity.MemberPriceEntity;
import com.icode.gulimall.coupon.entity.SkuLadderEntity;
import com.icode.gulimall.coupon.service.MemberPriceService;
import com.icode.gulimall.coupon.service.SkuLadderService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.icode.common.utils.PageUtils;
import com.icode.common.utils.Query;

import com.icode.gulimall.coupon.dao.SkuFullReductionDao;
import com.icode.gulimall.coupon.entity.SkuFullReductionEntity;
import com.icode.gulimall.coupon.service.SkuFullReductionService;


@Service("skuFullReductionService")
public class SkuFullReductionServiceImpl extends ServiceImpl<SkuFullReductionDao, SkuFullReductionEntity> implements SkuFullReductionService {

    @Autowired
    SkuLadderService skuLadderService;

    @Autowired
    MemberPriceService memberPriceService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuFullReductionEntity> page = this.page(
                new Query<SkuFullReductionEntity>().getPage(params),
                new QueryWrapper<SkuFullReductionEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveSpuReduction(SkuReductionTo skuReductionTo) {
        //1、保存满减大招、会员价
        // gulimall_sms -> sms_sku_ladder
        if (skuReductionTo.getFullCount() > 0) {
            SkuLadderEntity skuLaddrEntity = new SkuLadderEntity();
            BeanUtils.copyProperties(skuReductionTo, skuLaddrEntity);
            skuLadderService.save(skuLaddrEntity);
        }
        // gulimall_sms -> sms_sku_full_reduction
        if (skuReductionTo.getFullPrice().compareTo(new BigDecimal("0")) == 1) {
            SkuFullReductionEntity fullReductionEntity = new SkuFullReductionEntity();
            BeanUtils.copyProperties(skuReductionTo, fullReductionEntity);
            this.save(fullReductionEntity);
        }
        // gulimall_sms -> sms_member_price
        List<MemberPrice> memberPrice = skuReductionTo.getMemberPrice();
        List<MemberPriceEntity> collect = memberPrice.stream()
                .filter(item ->
                        item.getPrice().compareTo(new BigDecimal("0")) == 1
                )
                .map(item -> {
                    MemberPriceEntity memberPriceEntity = new MemberPriceEntity();
                    memberPriceEntity.setSkuId(skuReductionTo.getSkuId());
                    memberPriceEntity.setMemberLevelId(item.getId());
                    memberPriceEntity.setMemberLevelName(item.getName());
                    memberPriceEntity.setMemberPrice(item.getPrice());
                    memberPriceEntity.setAddOther(1);
                    return memberPriceEntity;
                }).collect(Collectors.toList());
        memberPriceService.saveBatch(collect);
    }

}